use strict;

use lib '/var/www/perl/galleri';

use Galleri;
my $g = new Galleri();

$g->add_content
(
 subject => "Registreringsskjema",
 body    => 
 [
  q{Fyll inn info i skjemaet nedenfor og send inn.},
  
  q{<form name="registration" onsubmit="return false">
       <table id="profile">
       <tr><td>Brukernavn:</td><td><input class="profile" id="username"></td></tr>
       <tr><td>Email:</td><td><input class="profile" id="email"></td></tr>
       <tr><td>Passord:</td><td><input type="password" class="profile" id="pass1"></td></tr>
       <tr><td>Gjenta Passord:</td><td><input type="password" class="profile" id="pass2"></td></tr>
       <tr><td colspan="2"><input style="width: 400px;" type="button" value="Oppdater" onclick="document.getElementById('messagebox').innerHTML='';jsRegisterProfile(['username','email','pass1','pass2'],[register_form])"></td></tr>
       </table>


</form>
<div id="messagebox"></div>
<script language="javascript">
function register_form(input){
  
  errors = Array();
  
  pairs = input.split("&");
  for( i=0; i<pairs.length; i++ ){
    nv = pairs[i].split("=");
    n=nv[0];
    v=nv[1];
    
    if(n=="error")
      errors[ errors.length ] = v;
    
  }
  
  m = document.getElementById("messagebox");
  
  if(errors.length > 0){
    m.style.color = "red";
    m.innerHTML = errors.join("<br/>\\n");
  }
  else{
    m.style.color = "blue";
    m.innerHTML = "Registrering OK";
    document.registration.username.value="";
    document.registration.email.value="";
    document.registration.pass1.value="";
    document.registration.pass2.value="";
  }
  
}
</script>
}
 ],
);

my $pjx = $g->pjx;
$pjx->register( jsRegisterProfile => \&jsRegisterProfile );

sub jsRegisterProfile {
  $g->register_user(@_);
}

$g->output;
